home *** CD-ROM | disk | FTP | other *** search
/ Tech Win 1995 November / CD [TECH_B].bin / tech_b / delphi / trial / disk4 / doc.pak / GRIDS.INT < prev    next >
Encoding:
Text File  |  1995-08-08  |  17.1 KB  |  429 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {       Delphi Visual Component Library                 }
  4. {                                                       }
  5. {       Copyright (c) 1995 Borland International        }
  6. {                                                       }
  7. {*******************************************************}
  8.  
  9. unit Grids;
  10.  
  11. interface
  12.  
  13. uses SysUtils, Messages, WinTypes, Classes, Graphics, Menus, Controls, Forms,
  14.   StdCtrls, Mask;
  15.  
  16. const
  17.   MaxCustomExtents = 65520 div SizeOf(Integer);
  18.  
  19. type
  20.   EInvalidGridOperation = class(Exception);
  21.  
  22.   { Internal grid types }
  23.  
  24.   TGridDrawInfo = record
  25.     EffectiveHorzLineWidth, EffectiveVertLineWidth: Integer;
  26.     FixedBoundaryX, FixedBoundaryY: Integer;
  27.     GridBoundaryX, GridBoundaryY: Integer;
  28.     GridHeight, GridWidth: Integer;
  29.     LastFullVisibleCol, LastFullVisibleRow: Longint;
  30.     FullVisBoundaryX, FullVisBoundaryY: Integer;
  31.   end;
  32.  
  33.   TGridState = (gsNormal, gsSelecting, gsRowSizing, gsColSizing,
  34.     gsRowMoving, gsColMoving);
  35.  
  36.   { TInplaceEdit }
  37.   { The inplace editor is not intended to be used outside the grid }
  38.  
  39.   TCustomGrid = class;
  40.  
  41.   TInplaceEdit = class(TCustomMaskEdit)
  42.   protected
  43.     procedure CreateParams(var Params: TCreateParams); override;
  44.     procedure DblClick; override;
  45.     function EditCanModify: Boolean; override;
  46.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  47.     procedure KeyPress(var Key: Char); override;
  48.     procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  49.     procedure WndProc(var Message: TMessage); override;
  50.   public
  51.     constructor Create(AOwner: TComponent);
  52.     procedure Deselect;
  53.     procedure Hide;
  54.     procedure Invalidate;
  55.     procedure Move(const Loc: TRect);
  56.     function PosEqual(const Rect: TRect): Boolean;
  57.     procedure SetFocus;
  58.     procedure UpdateLoc(const Loc: TRect);
  59.     function Visible: Boolean;
  60.   end;
  61.  
  62.   { TCustomGrid }
  63.  
  64.   { TCustomGrid is an abstract base class that can be used to implement
  65.     general purpose grid style controls.  The control will call DrawCell for
  66.     each of the cells allowing the derived class to fill in the contents of
  67.     the cell.  The base class handles scrolling, selection, cursor keys, and
  68.     scrollbars.
  69.       DrawCell
  70.         Called by Paint. If DefaultDrawing is true the font and brush are
  71.         intialized to the control font and cell color.  The cell is prepainted
  72.         in the cell color and a focus rect is draw in the focused cell after
  73.         DrawCell returns.  The state passed will reflect whether the cell is
  74.         a fixed cell, the focused cell or in the selection.
  75.       SizeChanged
  76.         Called when the size of the grid has changed.
  77.       BorderStyle
  78.         Allows a single line border to be drawn around the control.
  79.       Col
  80.         The current column of the focused cell (runtime only).
  81.       ColCount
  82.         The number of columns in the grid.
  83.       ColWidths
  84.         The width of each column (up to a maximum MaxCustomExtents, runtime
  85.         only).
  86.       DefaultColWidth
  87.         The default column width.  Changing this value will throw away any
  88.         customization done either visually or through ColWidths.
  89.       DefaultDrawing
  90.         Indicates whether the Paint should do the drawing talked about above in
  91.         DrawCell.
  92.       DefaultRowHeight
  93.         The default row height.  Changing this value will throw away any
  94.         customization done either visually or through RowHeights.
  95.       FixedCols
  96.         The number of non-scrolling columns.  This value must be at least one
  97.         below ColCount.
  98.       FixedRows
  99.         The number of non-scrolling rows.  This value must be at least one
  100.         below RowCount.
  101.       GridLineWidth
  102.         The width of the lines drawn between the cells.
  103.       LeftCol
  104.         The index of the left most displayed column (runtime only).
  105.       Options
  106.         The following options are available:
  107.           goFixedHorzLine:     Draw horizontal grid lines in the fixed cell area.
  108.           goFixedVertLine:     Draw veritical grid lines in the fixed cell area.
  109.           goHorzLine:          Draw horizontal lines between cells.
  110.           goVertLine:          Draw vertical lines between cells.
  111.           goRangeSelect:       Allow a range of cells to be selected.
  112.           goDrawFocusSelected: Draw the focused cell in the selected color.
  113.           goRowSizing:         Allows rows to be individually resized.
  114.           goColSizing:         Allows columns to be individually resized.
  115.           goRowMoving:         Allows rows to be moved with the mouse
  116.           goColMoving:         Allows columns to be moved with the mouse.
  117.           goEditing:           Places an edit control over the focused cell.
  118.           goAlwaysShowEditor:  Always shows the editor in place instead of
  119.                                waiting for a keypress or F2 to display it.
  120.           goTabs:              Enables the tabbing between columns.
  121.           goRowSelect:         Selection and movement is done a row at a time.
  122.       Row
  123.         The row of the focused cell (runtime only).
  124.       RowCount
  125.         The number of rows in the grid.
  126.       RowHeights
  127.         The hieght of each row (up to a maximum MaxCustomExtents, runtime
  128.         only).
  129.       ScrollBars
  130.         Determines whether the control has scrollbars.
  131.       Selection
  132.         A TGridRect of the current selection.
  133.       TopLeftChanged
  134.         Called when the TopRow or LeftCol change.
  135.       TopRow
  136.         The index of the top most row displayed (runtime only)
  137.       VisibleColCount
  138.         The number of columns fully displayed.  There could be one more column
  139.         partially displayed.
  140.       VisibleRowCount
  141.         The number of rows fully displayed.  There could be one more row
  142.         partially displayed. }
  143.  
  144.   TGridOption = (goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
  145.     goRangeSelect, goDrawFocusSelected, goRowSizing, goColSizing, goRowMoving,
  146.     goColMoving, goEditing, goTabs, goRowSelect,
  147.     goAlwaysShowEditor, goThumbTracking);
  148.   TGridOptions = set of TGridOption;
  149.   TGridDrawState = set of (gdSelected, gdFocused, gdFixed);
  150.   TGridScrollDirection = set of (sdLeft, sdRight, sdUp, sdDown);
  151.  
  152.   TGridCoord = record
  153.     X: Longint;
  154.     Y: Longint;
  155.   end;
  156.  
  157.   TGridRect = record
  158.     case Integer of
  159.       0: (Left, Top, Right, Bottom: Longint);
  160.       1: (TopLeft, BottomRight: TGridCoord);
  161.   end;
  162.  
  163.   TSelectCellEvent = procedure (Sender: TObject; Col, Row: Longint;
  164.     var CanSelect: Boolean) of object;
  165.   TDrawCellEvent = procedure (Sender: TObject; Col, Row: Longint;
  166.     Rect: TRect; State: TGridDrawState) of object;
  167.  
  168.   TCustomGrid = class(TCustomControl)
  169.   protected
  170.     FGridState: TGridState;
  171.     FSaveCellExtents: Boolean;
  172.     function CreateEditor: TInplaceEdit; virtual;
  173.     procedure CreateParams(var Params: TCreateParams); override;
  174.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  175.     procedure KeyPress(var Key: Char); override;
  176.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  177.       X, Y: Integer); override;
  178.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  179.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  180.       X, Y: Integer); override;
  181.     procedure AdjustSize(Index, Amount: Longint; Rows: Boolean); dynamic;
  182.     function BoxRect(ALeft, ATop, ARight, ABottom: Longint): TRect;
  183.     procedure DoExit; override;
  184.     function CellRect(ACol, ARow: Longint): TRect;
  185.     function CanEditAcceptKey(Key: Char): Boolean; dynamic;
  186.     function CanGridAcceptKey(Key: Word; Shift: TShiftState): Boolean; dynamic;
  187.     function CanEditModify: Boolean; dynamic;
  188.     function CanEditShow: Boolean; virtual;
  189.     function GetEditText(ACol, ARow: Longint): string; dynamic;
  190.     procedure SetEditText(ACol, ARow: Longint; const Value: string); dynamic;
  191.     function GetEditMask(ACol, ARow: Longint): string; dynamic;
  192.     function GetEditLimit: Integer; dynamic;
  193.     function GetGridWidth: Integer;
  194.     function GetGridHeight: Integer;
  195.     procedure HideEditor;
  196.     procedure ShowEditor;
  197.     procedure ShowEditorChar(Ch: Char);
  198.     procedure InvalidateEditor;
  199.     procedure ColumnMoved(FromIndex, ToIndex: Longint); dynamic;
  200.     procedure RowMoved(FromIndex, ToIndex: Longint); dynamic;
  201.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  202.       AState: TGridDrawState); virtual; abstract;
  203.     procedure DefineProperties(Filer: TFiler); override;
  204.     function MouseCoord(X, Y: Integer): TGridCoord;
  205.     procedure MoveColRow(ACol, ARow: Longint; MoveAnchor, Show: Boolean);
  206.     function SelectCell(ACol, ARow: Longint): Boolean; virtual;
  207.     procedure SizeChanged(OldColCount, OldRowCount: Longint); dynamic;
  208.     function Sizing(X, Y: Integer): Boolean;
  209.     procedure ScrollData(DX, DY: Integer);
  210.     procedure InvalidateCell(ACol, ARow: Longint);
  211.     procedure InvalidateRow(ARow: Longint);
  212.     procedure TopLeftChanged; dynamic;
  213.     procedure TimedScroll(Direction: TGridScrollDirection); dynamic;
  214.     procedure Paint; override;
  215.     procedure ColWidthsChanged; dynamic;
  216.     procedure RowHeightsChanged; dynamic;
  217.     property BorderStyle: TBorderStyle default bsSingle;
  218.     property Col: Longint;
  219.     property Color default clWindow;
  220.     property ColCount: Longint default 5;
  221.     property ColWidths[Index: Longint]: Integer;
  222.     property DefaultColWidth: Integer default 64;
  223.     property DefaultDrawing: Boolean default True;
  224.     property DefaultRowHeight: Integer default 24;
  225.     property EditorMode: Boolean;
  226.     property FixedColor: TColor default clBtnFace;
  227.     property FixedCols: Integer default 1;
  228.     property FixedRows: Integer default 1;
  229.     property GridHeight: Integer;
  230.     property GridLineWidth: Integer default 1;
  231.     property GridWidth: Integer;
  232.     property InplaceEditor: TInplaceEdit;
  233.     property LeftCol: Longint;
  234.     property Options: TGridOptions default [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect];
  235.     property ParentColor default False;
  236.     property Row: Longint;
  237.     property RowCount: Longint default 5;
  238.     property RowHeights[Index: Longint]: Integer;
  239.     property ScrollBars: TScrollStyle default ssBoth;
  240.     property Selection: TGridRect;
  241.     property TabStops[Index: Longint]: Boolean;
  242.     property TopRow: Longint;
  243.     property VisibleColCount: Integer;
  244.     property VisibleRowCount: Integer;
  245.   public
  246.     constructor Create(AOwner: TComponent); override;
  247.     destructor Destroy; override;
  248.   published
  249.     property TabStop default True;
  250.   end;
  251.  
  252.   { TDrawGrid }
  253.  
  254.   { A grid relies on the OnDrawCell event to display the cells.
  255.      CellRect
  256.        This method returns control relative screen coordinates of the cell or
  257.        an empty rectangle if the cell is not visible.
  258.      EditorMode
  259.        Setting to true shows the editor, as if the F2 key was pressed, when
  260.        goEditing is turned on and goAlwaysShowEditor is turned off.
  261.      MouseToCell
  262.        Takes control relative screen X, Y location and fills in the column and
  263.        row that contain that point.
  264.      OnColumnMoved
  265.        Called when the user request to move a column with the mouse when
  266.        the goColMoving option is on.
  267.      OnDrawCell
  268.        This event is passed the same information as the DrawCell method
  269.        discussed above.
  270.      OnGetEditMask
  271.        Called to retrieve edit mask in the inplace editor when goEditing is
  272.        turned on.
  273.      OnGetEditText
  274.        Called to retrieve text to edit when goEditing is turned on.
  275.      OnRowMoved
  276.        Called when the user request to move a row with the mouse when
  277.        the goRowMoving option is on.
  278.      OnSetEditText
  279.        Called when goEditing is turned on to reflect changes to the text
  280.        made by the editor.
  281.      OnTopLeftChanged
  282.        Invoked when TopRow or LeftCol change. }
  283.  
  284.   TGetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; var Value: string) of object;
  285.   TSetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; const Value: string) of object;
  286.   TMovedEvent = procedure (Sender: TObject; FromIndex, ToIndex: Longint) of object;
  287.  
  288.   TDrawGrid = class(TCustomGrid)
  289.   protected
  290.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  291.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  292.       AState: TGridDrawState); override;
  293.     function GetEditMask(ACol, ARow: Longint): string; override;
  294.     function GetEditText(ACol, ARow: Longint): string; override;
  295.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  296.     function SelectCell(ACol, ARow: Longint): Boolean; override;
  297.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  298.     procedure TopLeftChanged; override;
  299.   public
  300.     function CellRect(ACol, ARow: Longint): TRect;
  301.     procedure MouseToCell(X, Y: Integer; var ACol, ARow: Longint);
  302.     property Canvas;
  303.     property Col;
  304.     property ColWidths;
  305.     property EditorMode;
  306.     property GridHeight;
  307.     property GridWidth;
  308.     property LeftCol;
  309.     property Selection;
  310.     property Row;
  311.     property RowHeights;
  312.     property TabStops;
  313.     property TopRow;
  314.   published
  315.     property Align;
  316.     property BorderStyle;
  317.     property Color;
  318.     property ColCount;
  319.     property Ctl3D;
  320.     property DefaultColWidth;
  321.     property DefaultRowHeight;
  322.     property DefaultDrawing;
  323.     property DragCursor;
  324.     property DragMode;
  325.     property Enabled;
  326.     property FixedColor;
  327.     property FixedCols;
  328.     property FixedRows;
  329.     property Font;
  330.     property GridLineWidth;
  331.     property Options;
  332.     property ParentColor;
  333.     property ParentCtl3D;
  334.     property ParentFont;
  335.     property ParentShowHint;
  336.     property PopupMenu;
  337.     property RowCount;
  338.     property ScrollBars;
  339.     property ShowHint;
  340.     property TabOrder;
  341.     property TabStop;
  342.     property Visible;
  343.     property VisibleColCount;
  344.     property VisibleRowCount;
  345.     property OnClick;
  346.     property OnColumnMoved: TMovedEvent;
  347.     property OnDblClick;
  348.     property OnDragDrop;
  349.     property OnDragOver;
  350.     property OnDrawCell: TDrawCellEvent;
  351.     property OnEndDrag;
  352.     property OnEnter;
  353.     property OnExit;
  354.     property OnGetEditMask: TGetEditEvent;
  355.     property OnGetEditText: TGetEditEvent;
  356.     property OnKeyDown;
  357.     property OnKeyPress;
  358.     property OnKeyUp;
  359.     property OnMouseDown;
  360.     property OnMouseMove;
  361.     property OnMouseUp;
  362.     property OnRowMoved: TMovedEvent;
  363.     property OnSelectCell: TSelectCellEvent;
  364.     property OnSetEditText: TSetEditEvent;
  365.     property OnTopLeftChanged: TNotifyEvent;
  366.   end;
  367.  
  368.   { TStringGrid }
  369.  
  370.   { TStringGrid adds to TDrawGrid the ability to save a string and associated
  371.     object (much like TListBox).  It also adds to the DefaultDrawing the drawing
  372.     of the string associated with the current cell.
  373.       Cells
  374.         A ColCount by RowCount array of strings which are associated with each
  375.         cell.  By default, the string is drawn into the cell before OnDrawCell
  376.         is called.  This can be turned off (along with all the other default
  377.         drawing) by setting DefaultDrawing to false.
  378.       Cols
  379.         A TStrings object that contains the strings and objects in the column
  380.         indicated by Index.  The TStrings will always have a count of RowCount.
  381.         If a another TStrings is assigned to it, the strings and objects passed
  382.         RowCount are ignored.
  383.       Objects
  384.         A ColCount by Rowcount array of TObject's associated with each cell.
  385.         Object put into this array will *not* be destroyed automatically when
  386.         the grid is destroyed.
  387.       Rows
  388.         A TStrings object that contains the strings and objects in the row
  389.         indicated by Index.  The TStrings will always have a count of ColCount.
  390.         If a another TStrings is assigned to it, the strings and objects passed
  391.         ColCount are ignored. }
  392.  
  393.   TStringGrid = class;
  394.  
  395.   TStringGridStrings = class(TStrings)
  396.   protected
  397.     procedure Clear; override;
  398.     function Add(const S: string): Integer; override;
  399.     function Get(Index: Integer): string; override;
  400.     function GetCount: Integer; override;
  401.     function GetObject(Index: Integer): TObject; override;
  402.     procedure Put(Index: Integer; const S: string); override;
  403.     procedure PutObject(Index: Integer; AObject: TObject); override;
  404.     procedure SetUpdateState(Updating: Boolean); override;
  405.   public
  406.     constructor Create(AGrid: TStringGrid; AIndex: Longint);
  407.     procedure Assign(Source: TPersistent); override;
  408.   end;
  409.  
  410.  
  411.   TStringGrid = class(TDrawGrid)
  412.   protected
  413.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  414.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  415.       AState: TGridDrawState); override;
  416.     function GetEditText(ACol, ARow: Longint): string; override;
  417.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  418.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  419.   public
  420.     constructor Create(AOwner: TComponent); override;
  421.     destructor Destroy; override;
  422.     property Cells[ACol, ARow: Integer]: string;
  423.     property Cols[Index: Integer]: TStrings;
  424.     property Objects[ACol, ARow: Integer]: TObject;
  425.     property Rows[Index: Integer]: TStrings;
  426.   end;
  427.  
  428. implementation
  429.